package com.andy.buyu.server.socket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

public class HeartbeatHandler extends ChannelInboundHandlerAdapter{
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Override
	public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
		if(evt instanceof IdleStateEvent) {
			NettySocketSession session = ctx.channel().attr(SocketFrameHandler.nssKey).get();
			if(session != null && session.getUser() != null){
				logger.info("====>Heartbeat: User:{}", session.getUser().getId());
			}else{
				logger.info("====>Heartbeat:");
			}
			SendUtil.send400(session);
		}else {
			super.userEventTriggered(ctx, evt);
		}
	}
}